package com.sqly.doc.ble.xty;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.sqly.doc.R;
import com.sqly.doc.util.MyApplication;
import com.sqly.doc.util.ToastUtil;

/**
 * Created by 袁从斌 on 2019-08-05.
 */
public class InputNumDialog extends DialogFragment {

	private EditText mEtNum;

	private boolean mIsShowDeleteHistory = false;
	private OnDialogClickListener mOnDialogClickListener;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCancelable(false);
	}

	public void setShowDeleteHistory(boolean showDeleteHistory) {
		mIsShowDeleteHistory = showDeleteHistory;
	}

	public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
		mOnDialogClickListener = onDialogClickListener;
	}

	public interface OnDialogClickListener{
		void onCancel();

		void onConfirm(int num);
	}
	@Nullable
	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		Window window = getDialog().getWindow();
		WindowManager.LayoutParams layoutParams = window.getAttributes();
		layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
		layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		layoutParams.gravity = Gravity.CENTER;
		window.setAttributes(layoutParams);
		View view = inflater.inflate(R.layout.dialog_num, container, false);
		mEtNum = view.findViewById(R.id.etNum);
		mEtNum.setHint(mIsShowDeleteHistory ? getString(R.string.delete_history_by_count) :
				getString(R.string.get_history_by_count));
		view.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mOnDialogClickListener!=null){
					mOnDialogClickListener.onCancel();
				}
				dismiss();
			}
		});
		view.findViewById(R.id.tvConfirm).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String s = mEtNum.getText().toString();
				if (TextUtils.isEmpty(s)){
					ToastUtil.showShortToast(MyApplication.getApp(),getString(R.string.num_error));
					return;
				}
				int num = Integer.parseInt(s);
				if (num<=0 || num>100){
					ToastUtil.showShortToast(MyApplication.getApp(),getString(R.string.num_error));
					return;
				}
				if (mOnDialogClickListener!=null){
					mOnDialogClickListener.onConfirm(num);
				}
				dismiss();
			}
		});
		return view;
	}
}
